﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VerifySystem
{
    /// <summary>
    /// 卡号验证接口
    /// </summary>
    public interface ICardVerify
    {
        /// <summary>
        /// 二要素验证
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        Response ValidateCard2E(string cardNo, string name);
        /// <summary>
        /// 三要素验证
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="name"></param>
        /// <param name="idNo"></param>
        /// <returns></returns>
        Response ValidateCard3E(string cardNo, string name, string idNo);
        /// <summary>
        /// 四要素验证
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="name"></param>
        /// <param name="idNo"></param>
        /// <param name="PhoneNo"></param>
        /// <returns></returns>
        Response ValidateCard4E(string cardNo, string name, string idNo, string PhoneNo);
    }
    /// <summary>
    /// 银联卡号验证类
    /// </summary>
    public class YinlianCardVerify : ICardVerify
    {
        public Response ValidateCard2E(string cardNo, string name)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }

        public Response ValidateCard3E(string cardNo, string name, string idNo)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }

        public Response ValidateCard4E(string cardNo, string name, string idNo, string PhoneNo)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }
    }
    /// <summary>
    /// Paypal卡号验证类
    /// </summary>
    public class PaypalCardVerify : ICardVerify
    {
        public Response ValidateCard2E(string cardNo, string name)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }

        public Response ValidateCard3E(string cardNo, string name, string idNo)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }

        public Response ValidateCard4E(string cardNo, string name, string idNo, string PhoneNo)
        {
            return new Response
            {
                Code = 0,
                Message = "验证成功！"
            };
        }
    }

    public class VerifyMgr
    {
        /// <summary>
        /// 卡号验证类工厂
        /// </summary>
        private Dictionary<string, Func<ICardVerify>> _factories;

        public VerifyMgr()
        {
            _factories = new Dictionary<string, Func<ICardVerify>> {
                { InType.Yinlian,()=>new YinlianCardVerify()},
                { InType.Paypal,()=>new PaypalCardVerify()}
            };
        }
        /// <summary>
        /// 获取卡号验证类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public ICardVerify GetCardVerify(string type)
        {
            if (_factories.ContainsKey(type))
            {
                var factories = _factories[type];
                return factories();
            }
            else
            {
                throw new ArgumentException("Invalid type", nameof(type));
            }
        }
        /// <summary>
        /// 二要素验证（需传入卡号和姓名）
        /// </summary>
        /// <param name="cardVerify"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public Response CardVerify2E(ICardVerify cardVerify, VerifyParam param)
        {
            if (string.IsNullOrEmpty(param.CardNo) || string.IsNullOrEmpty(param.Name))
            {
                return new Response
                {
                    Code = -1,
                    Message = "卡号或姓名为空,无法验证"
                };
            }
            return cardVerify.ValidateCard2E(param.CardNo, param.Name);
        }
        /// <summary>
        /// 三要素验证（需传入卡号、姓名和身份证号）
        /// </summary>
        /// <param name="cardVerify"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public Response CardVerify3E(ICardVerify cardVerify, VerifyParam param)
        {
            if (string.IsNullOrEmpty(param.CardNo) || string.IsNullOrEmpty(param.Name) || string.IsNullOrEmpty(param.IdNo))
            {
                return new Response
                {
                    Code = -1,
                    Message = "卡号、姓名或身份证号为空,无法验证"
                };
            }
            return cardVerify.ValidateCard3E(param.CardNo, param.Name, param.IdNo);
        }
        /// <summary>
        /// 四要素验证（需传入卡号、姓名、身份证号和手机号）
        /// </summary>
        /// <param name="cardVerify"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public Response CardVerify4E(ICardVerify cardVerify, VerifyParam param)
        {
            if (string.IsNullOrEmpty(param.CardNo) || string.IsNullOrEmpty(param.Name) || string.IsNullOrEmpty(param.IdNo) || string.IsNullOrEmpty(param.PhoneNo))
            {
                return new Response
                {
                    Code = -1,
                    Message = "卡号、姓名、身份证号或手机号为空,无法验证"
                };
            }
            return cardVerify.ValidateCard4E(param.CardNo, param.Name, param.IdNo, param.IdNo);
        }
    }

    public class VerifyParam
    {
        /// <summary>
        /// 银行卡号
        /// </summary>
        public string CardNo { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        public string PhoneNo { get; set; }

        /// <summary>
        /// 身份证号
        /// </summary>
        public string IdNo { get; set; }
    }

    public class Response
    {
        /// <summary>
        /// 应答码（0：验证成功 -1：验证失败）
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string Message { get; set; }
    }

    public static class InType
    {
        public const string Yinlian = "A";

        public const string Paypal = "B";
    }
}
